# brew(1) completion                                        -*- shell-script -*-

prog=$(basename $BASH_SOURCE)
__brew()
{
    local cur prev words cword
    local narg
    _init_completion || return

    for w in "${words[@]}"; do
        [[ $w =~ ^-.* ]] || ((narg++))
    done

    local opts=$( _parse_help "$1" )

    if [[ $narg -gt 2 ]]; then
        args=$( $prog ${words[1]} -h | grep '^  *' | egrep -o -- '--*[a-zA-Z0-9]+' )
        COMPREPLY=( $( compgen -W "$args" -- "$cur" ) )
    else
        if [[ "$prev" = ${words[1]} ]]; then
            args=$( $prog ${words[1]} -h | grep '^  *' | egrep -o -- '--*[a-zA-Z0-9]+' )
            COMPREPLY=( $( compgen -W "$args" -- "$cur" ) )
        else
            local subcmd=$( $prog help | awk '/     */{print $1}' )
            COMPREPLY=( $( compgen -W "$subcmd" -- "$cur" ) )
        fi
    fi
} &&
complete -F __brew brew

#echo $prog
# ex: ts=4 sw=4 et filetype=sh
